public class Genome
{
 
  Chromosome[] chromosomes;
  String genomeName;
  String genomeFileName;
  
  int numOfChr;
  
  float genomeX1, genomeY1, genomeX2, genomeY2;
  
  int totalGenomeLength;
  
  Genome( String genomeName, int numOfChr, float genomeX1, float genomeY1, float genomeX2, float genomeY2 ) {

     this.genomeName = genomeName;
     this.numOfChr = numOfChr;
     
     this.genomeX1 = genomeX1;
     this.genomeY1 = genomeY1;
     this.genomeX2 = genomeX2;
     this.genomeY2 = genomeY2;
     
     chromosomes = new Chromosome[ numOfChr ];
     
     println(genomeName + " " + numOfChr + " " + genomeX1 + " " + genomeY1 + " " + genomeX2 + " " + genomeY2);
     
     //READ FROM FILE
     String[] rows = loadStrings(genomeName); 
    
     String[] columns = split(rows[0], '\t'); 
     String[] columnNames = subset(columns, 1); // upper-left corner ignored
     scrubQuotes(columnNames);
     int columnCount = columnNames.length;
      
     String[] rowNames = new String[rows.length-1];
     String[][] data = new String[rows.length-1][];
     int rowCount = 0;
      
     // start reading at row 1, because the first row was only the column headers
     for (int i = 1; i < rows.length; i++) {
        if (trim(rows[i]).length() == 0) {
          continue; // skip empty rows
        }
        if (rows[i].startsWith("#")) {
          continue;  // skip comment lines
        }

        // split the row on the commas
        String[] pieces = split(rows[i], '\t');
        scrubQuotes(pieces);
      
        // copy row title
        rowNames[rowCount] = pieces[0];
        // copy data into the table starting at pieces[1]
        data[rowCount] = subset(pieces, 0);

        // increment the number of valid rows found so far
        rowCount++;      
     }
      
     // resize the 'data' array as necessary
     data = (String[][]) subset(data, 0, rowCount); 
     
     //find total genome length
     totalGenomeLength = 0;
     for(int i = 0; i < data.length; i++)
     {
       totalGenomeLength += parseInt( data[i][2] );
     }
     
     //CREATE AND PLACE CHROMOSOMES
     int prevChrLength = 0; //hold chr length up to this point
     for(int i = 0; i < data.length; i++) //skip the first 
     {
        int chrNum = parseInt( data[i][0] );
        //int chrStart = parseInt( data[i][1] );
        int chrLength = parseInt( data[i][2] );
        
        float chrX1 = findChromosomeX1( chrNum, chrLength, prevChrLength );
        float chrX2 = findChromosomeX2( chrNum, chrLength, prevChrLength );
       
        float chrY1 = genomeY1;
        float chrY2 = genomeY2;
         
         println("chr " + chrNum + " chrLength = " + chrLength + " chrX1 = " + chrX1 + " chrX2 = " + chrX2 );
         
        Chromosome c = new Chromosome( genomeName, chrNum, chrLength, chrX1, chrY1, chrX2, chrY2 );
        if( i >= numOfChr )
        {
          println("wrong input data format- lines in data file don't match num chromosomes");
          break;
        }
        chromosomes[i] = c; 
        
        prevChrLength+=chrLength;
     }
     
   }//end genome constructor
   
   void draw()
   {
     for(int i = 0 ; i < chromosomes.length; i++)
     {
       chromosomes[i].draw();
     }
   }
   
   float findChromosomeX1( int chrNum, int chrLength, int prevChrLength )
   {
     return ( (float) prevChrLength / (float) totalGenomeLength ) * genomeX2 ;
   }
   
   float findChromosomeX2( int chrNum, int chrLength, int prevChrLength )
   {
     int spacingBtwChr = 5;
     return ( ((float)prevChrLength+chrLength) / (float) totalGenomeLength ) * genomeX2 - spacingBtwChr ;
   }
    
   void scrubQuotes(String[] array) {
    for (int i = 0; i < array.length; i++) {
      if (array[i].length() > 2) {
        // remove quotes at start and end, if present
        if (array[i].startsWith("\"") && array[i].endsWith("\"")) {
          array[i] = array[i].substring(1, array[i].length() - 1);
        }
      }
      // make double quotes into single quotes
      array[i] = array[i].replaceAll("\"\"", "\"");
    }
  }//end scrubQuotes
 
  
}//end Genome
